<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              v-if="isAuth('module:business:inquiry:form')"
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_business_inquiry_form' })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:business:inquiry:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-input v-model="dataForm.keyword" :placeholder="$t('common.please_input') + $t('common.keyword')" clearable>
          </el-input>
        </div>
        <div class="input">
          <el-input v-model="dataForm.consignor__chinese_title" :placeholder="$t('common.please_input') + $t('organization.chinese_title')" clearable>
          </el-input>
        </div>
        <div class="input">
          <el-input v-model="dataForm.member__username" :placeholder="$t('common.please_input') + $t('member.username')" clearable>
          </el-input>
        </div>
        <div class="input">
          <el-select v-model="dataForm.inquiry_status" :placeholder="$t('common.please_select') + $t('business.inquiry.inquiry_status')" clearable>
            <el-option :label="$t('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in inquiryStatusList" :label="v.text" :key="k" :value="v.value">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select v-model="dataForm.type" :placeholder="$t('common.please_select') + $t('business.inquiry.type')" clearable>
            <el-option :label="$t('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in inquiryTypeList" :label="v.text" :key="k" :value="v.value">
            </el-option>
          </el-select>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.create_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
          </el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>
      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column prop="order_no" :label="$t('business.inquiry.order_no')" width="150">
          </el-table-column>

          <el-table-column :label="$t('business.inquiry.type')" width="80">
            <template v-slot="scope">
              <el-text>
                {{ scope.row.type == 1 ? '公开盘' : '指定盘' }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="$t('business.inquiry.consignor.info')" min-width="400">
            <template v-slot="scope">
              <dl class="table_dl" v-if="scope.row.consignor">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.consignor.logo" loading="lazy" lazy>
                    <template #error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  <el-link type="primary" :underline="false" @click="router.push({ name: 'module_organization_list', query: { keyword: scope.row.consignor.chinese_title } })">
                    {{ scope.row.consignor.chinese_title }}
                  </el-link>
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ scope.row.consignor.english_title }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="$t('business.inquiry.consignor.member.info')" width="260">
            <template v-slot="scope">
              <dl class="table_dl" v-if="scope.row.member">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.member.avatar" loading="lazy" lazy>
                    <template #error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  <el-link type="primary" :underline="false" @click="router.push({ name: 'module_member_list', query: { keyword: scope.row.member.chinese_nickname } })">
                    {{ scope.row.member.chinese_nickname }}
                  </el-link>
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ scope.row.member.username }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="$t('business.inquiry.load_time')" width="190">
            <template v-slot="scope">
              <el-text v-if="scope.row.start_time">
                {{ scope.row.start_time }} ~ {{ scope.row.end_time }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column prop="description" :label="$t('business.inquiry.description')" min-width="300">
            <template v-slot="scope">
              <el-text line-clamp="2">
                {{ scope.row.description }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column prop="valid_time" :label="$t('business.inquiry.valid_time')" width="160">
          </el-table-column>

          <el-table-column prop="view_total" :label="$t('business.inquiry.view_total')" width="110">
          </el-table-column>

          <el-table-column prop="offer_total" :label="$t('business.inquiry.offer_total')" width="110">
          </el-table-column>

          <el-table-column prop="inquiry_status" :label="$t('business.inquiry.inquiry_status')" width="80">
            <template v-slot="scope">
              <el-text tag="b" :type="scope.row.inquiry_status.value == 0 ? 'info' : ((scope.row.inquiry_status.value == 1 || scope.row.inquiry_status.value == 4 || scope.row.inquiry_status.value == 5 || scope.row.inquiry_status.value == 6) ? 'primary' : (scope.row.inquiry_status.value == 3 ? 'success' : 'danger'))">
                {{ scope.row.inquiry_status.text }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column prop="create_time" :label="i18n('business.inquiry.create_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="290">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:business:inquiry:view')"
                icon="View"
                @click="router.push({ name: 'module_business_inquiry_view', query: { id: scope.row.id } })">
                {{ i18n('common.view') }}
              </el-button>

              <el-button
                v-if="isAuth('module:business:inquiry:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>

              <el-dropdown class="more">
                <el-button icon="More">
                  {{ i18n('common.more') }}
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item v-if="isAuth('module:business:inquiry:shipowner:list')" @click="router.push({ name: 'module_business_inquiry_shipowner_list', query: { inquiry_id: scope.row.id } })">
                      {{ i18n('business.inquiry.shipowner.info') }}
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="business-inquiry-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/inquiry')

  const inquiryStatusList = ref([])

  const inquiryTypeList = ref([
    {'value': 1, 'text': '公开盘'},
    {'value': 2, 'text': '指定盘'},
  ])

  const dataForm = reactive([
    'keyword',
    'consignor__chinese_title',
    'member__username',
    'inquiry_status',
    'create_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2025-07-04
   *
   * 获取询价状态信息
   *
   * @return {[type]}
   */
  const loadInquiryStatusList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'inquiry_status',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        inquiryStatusList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    getDataList(model)

    loadInquiryStatusList()
  })
</script>
